﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CQRS.Reports.SqlQueryBuilder
{
    public class SqlInsertStatementBuilder : ISqlInsertStatementBuilder
    {
        #region ISqlInsertStatementBuilder Members

        public string CreateSqlInsertStatementFromDto<TDto>() where TDto : Report
        {
            return GetInsertString<TDto>();
        }

        #endregion

        private static string GetInsertString<TDto>()
        {
            Type type = typeof (TDto);
            IEnumerable<PropertyInfo> properties =
                type.GetProperties().Where(x => !x.PropertyType.IsGenericType).ToList();
            string tableName = type.Name;

            return string.Format("INSERT INTO {0} ({1}) VALUES ({2});",
                                 tableName,
                                 string.Join(",", properties.Select(x => x.Name).ToArray()),
                                 string.Join(",",
                                             properties.Select(x => string.Format("@{0}", x.Name.ToLower())).ToArray()));
        }
    }
}